Variable Attributes
All IDL variables have special attributes that return information about the variable. These attributes are equivalent to calling the N_ELEMENTS, SIZE, or TYPENAME functions.
The following variable attributes are available:
Attribute | Definition |
---|---|
DIM | An array giving the dimensions (0 for scalars). |
LENGTH | The number of elements in the variable. |
NDIM | An integer giving the number of dimensions (0 for scalars). |
TNAME | A string giving the raw IDL type name. For structures this returns "STRUCT", while for objects this returns "OBJREF". |
TYPECODE | An integer giving the IDL type code. |
TYPENAME | A string giving the IDL type name. For structures and objects this returns the actual structure or class name. |
TYPESIZE | An integer giving the size in bytes of a single element (always 0 for strings and structures). |
Note: For structures and objects, if the structure or object contains a field with the same name as an attribute, then IDL will correctly retrieve the structure field and you will not be able to access the attribute.
Note: For most variables, the TNAME and TYPENAME attributes will return the same result. For structures and objects the TYPENAME will return the actual structure or class name, just like the TYPENAME function.
Examples
Scalars
First print the attributes for a scalar variable:
var = 3.14d
print, var.length
print, var.typename
print, var.typecode
print, var.ndim
print, var.dim
print, var.typesize
IDL prints:
1
DOUBLE
5
0
0
8
Arrays
Next, print the attributes for an array:
var = RANDOMU(seed, 100, 50, 3)
print, var.length
print, var.typename
print, var.typecode
print, var.ndim
print, var.dim
print, var.typesize
IDL prints:
15000
FLOAT
4
3
100 50 3
4
Structure Fields
The attributes can also be used on fields within structures. Here we use the LENGTH attribute as the loop limit:
var = {mystruct, field1: {substruct, subfield: LONARR(1000)}}
for i=0, var.field1.subfield.LENGTH-1 do var.field1.subfield[i]=i
; Use our static TOTAL method
PRINT, var.field1.subfield.total()
IDL prints:
499500.
Objects
For objects that inherit from IDL_Object, the LENGTH, NDIM, and DIM attributes will be taken from the overloaded ::Size method. For example:
h = HASH('key1', 4, 'key2', 5, 'key3', 6)
PRINT, h.length, h.ndim, h.dim, h.typecode, h.tname, h.typename
IDL prints:
3
1
3
11
OBJREF
HASH
Notice that the TNAME and TYPENAME attributes are different for objects.
Typesize
The TYPESIZE attribute may be useful when performing unformatted input/output, or converting variables to/from bytes. For example:
IDL> x = [!values.f_infinity, !values.f_nan]
IDL> y = byte(x, 0, x.length * x.typesize)
IDL> print, y
0 0 128 127 0 0 192 127
IDL> print, float(y, 0, y.length / x.typesize)
Inf NaN
Note: The TYPESIZE attribute is always 0 for strings and structures because these data types have varying lengths depending upon the stored data.
Version History
8.4 |
Introduced |
9.0 |
Allow array indexing for the DIM static attribute. Added TYPESIZE variable attribute. |